package com.wutianyi.jackson;

import java.io.File;
import java.io.IOException;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.TreeMapper;
import org.codehaus.jackson.node.ObjectNode;

public class TreeModel
{
    public static void main(String[] args) throws JsonProcessingException, IOException
    {
        ObjectMapper m = new ObjectMapper();

        JsonNode rootNode = m.readTree(TreeModel.class.getResourceAsStream("/user.json"));
        JsonNode nameNode = rootNode.path("name");
        String lastName = nameNode.path("last").getTextValue();
        if ("xmler".equalsIgnoreCase(lastName))
        {
            ((ObjectNode) nameNode).put("last", "Jsoner");
        }
        m.writeValue(new File("user-modified-tree.json"), rootNode);
        
    }
}
